<template>
    <div class="cms-site">
        <div style="height: 35px">
            <el-form :model="searchForm" :inline="true" size="mini">
                <el-form-item label="name">
                    <el-input v-model="searchForm.name"></el-input>
                </el-form-item>
                <el-form-item label="code">
                    <el-input v-model="searchForm.code"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="listSite">Search</el-button>
                    <el-button type="primary" @click="listSite">Refresh</el-button>
                    <el-button type="primary" @click="handleCreate">New</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
                border
                default-expand-all
                highlight-current-row
                :data="siteTable"
                row-key="ukId"
                height="500"
                size="mini"
                style="width: 100%"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
                    prop="name"
                    label="name"
                    width="200">
            </el-table-column>
            <el-table-column
                    prop="code"
                    label="code"
                    width="200">
            </el-table-column>
            <el-table-column
                    prop="url"
                    label="url"
                    width="*">
            </el-table-column>
            <el-table-column
                    prop="status"
                    label="status"
                    width="70">
                <template slot-scope="scope">
                    <el-switch
                            @change="statusChange($event, scope.row.ukId)"
                            :value="scope.row.status != '1'"
                            active-color="#909399"
                            inactive-color="#67c23a">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column
                    prop="sort"
                    label="sort"
                    width="60">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="145">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row.ukId)">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog title="站点管理" :visible.sync="showSiteForm" :close-on-click-modal="false" label-width="100px">
            <el-form ref="form" :model="siteForm" label-width="100px">
                <el-form-item label="站点编码">
                    <el-input v-model="siteForm.code"></el-input>
                </el-form-item>
                <el-form-item label="站点名称">
                    <el-input v-model="siteForm.name"></el-input>
                </el-form-item>
                <el-form-item label="站点地址">
                    <el-input v-model="siteForm.url"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="siteForm.status" placeholder="请选择状态">
                        <el-option
                                v-for="item in this.$store.state.Pabu.dict.cms.STATUS.dictItems"
                                :key="item.ukId"
                                :label="item.name"
                                :value="parseInt(item.value)">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="排序">
                    <el-input v-model="siteForm.sort" type="number" min="0" max="90000"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleSave">保 存</el-button>
                    <el-button @click="showSiteForm = false">取 消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
    import {deleteSite, listSite, saveSite, updateSite, updateSiteStatus} from "../../../api/cms/site";

    export default {
        name: "Site",
        data() {
            return {
                showSiteForm: false,
                showSiteUpdateForm: false,
                siteTable: [],
                searchForm: {},
                siteForm: {},
                pagination: {
                    total: 0,
                    size: 30,
                    current: 1,
                }
            }
        },
        methods: {
            handleCreate() {
                this.siteForm = {
                    status: 1
                }
                this.showSiteForm = true
            },
            handleEdit(row) {
                this.siteForm = row
                this.showSiteForm = true
            },
            handleUpdate() {
                updateSite(this.siteForm).then(result => {
                    this.listSite()
                    this.showSiteForm = false
                }).catch(error => {
                    console.log(error)
                })
            },
            statusChange (value, siteId) {
                let status = value ? 0 : 1
                updateSiteStatus(siteId, status).then(result => {
                    this.listSite()
                }).catch(error => {
                    console.log(error)
                })
            },
            handleSave() {
                saveSite(this.siteForm).then(result => {
                    this.listSite()
                    this.showSiteForm = false
                }).catch(error => {
                    console.log(error)
                })
            },
            handleDelete(ukId) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteSite(ukId).then(result => {
                        this.listSite()
                        this.showSiteForm = false
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }).catch(error => {
                        console.log(error)
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            listSite() {
                listSite(this.searchForm, 1, 30).then(result => {
                    this.siteTable = result.data.sites
                }).catch(error => {
                    console.log(error)
                })
            }
        },
        created() {
            this.listSite()
        }
    }
</script>

<style scoped>
    .cms-site {
        width: 100%;
        height: 100%;
    }

    /* element ui样式重写 */
    .cms-site > .el-table {
        height: calc(100% - 35px) !important;
    }

    .cms-site > .el-table > .el-table__body-wrapper {
        height: calc(100% - 35px) !important;
    }
</style>